import xlwt

#读取文本数据
#数据一和二的数据个数和处理方式都一样
date1 = []
date2 = []
with open("date1.csv","r") as f :
    date1 = f.readlines()
    f.close()

with open("date2.csv","r") as f :
    date2 = f.readlines()
    f.close()

#整理格式,去除分隔符
for i in range(len(date1)) :
    date1[i] = date1[i].split(",")
    date2[i] = date2[i].split(",")

date = [date1,date2]
n = len(date1) + 1
m = len(date1[0]) + 1
workbook = xlwt.Workbook(encoding='utf-8', style_compression=0)

#获取表格页
sheet1 = workbook.add_sheet('实验1', cell_overwrite_ok=False)
sheet2 = workbook.add_sheet('实验2', cell_overwrite_ok=False)
style = xlwt.XFStyle()

s = [sheet1,sheet2]
b = ["零件1","零件2","成品","拆解","利润"]

for pos in range(len(s)) :
    for i in range(1,m) :
       s[pos].write(0, i, b[i - 1])

    t = 17
    result = []#接受结果
    for i in range(1,n) :
        if i == t :
            re = []
            for j in range(1, m):
                re.append(date[pos][i - 1][j - 1])

            result.append(re)
            t += 17
            continue
        for j in range(1,m):
            if i < n :
               s[pos].write(i,j,date[pos][i - 1][j - 1])
    num = 1
    p = 1
    while p < n - 1 :
        s[pos].write(p, 0, f"情况{num}")
        num += 1
        p += 17

    #将最优结果写入表格
    for i in range(9,8 + m) :
       s[pos].write(0, i, b[i - 9])

    l = len(result) + 1
    for i in range(1,l) :
        for j in range(9,8 + m):
           s[pos].write(i,j,result[i - 1][j - 9])

    s[pos].write(3,7,"最大利润及其决策")
    p = 1
    for i in range(1,m + 1) :
        s[pos].write(i, 8, f"情况{p}")
        p += 1


    tmp_1 = ["说明","检测/拆解","不检测/不拆解"]
    tmp_2 = ["表示",1,0]
    tmp = []
    tmp.append(tmp_1)
    tmp.append(tmp_2)
    long = len(tmp) + 9
    wide = len(tmp[0]) + 8

    for i in range(9,long) :
        for j in range(8,wide) :
            s[pos].write(i, j, tmp[i - 9][j - 8])



workbook.save('question2_date.xls')
